//
//  LDCMyDateArrangeViewController.m
//  LaiKeDaoClient
//
//  Created by zouzhisheng on 15/9/28.
//  Copyright © 2015年 LKD. All rights reserved.
//

#import "LDCMyDateArrangeViewController.h"
#import "KDTitleView.h"
#import "KDCalendarPicker.h"
#import "SZCalendarCell.h"
#import "KDAddNumberView.h"
#import "KDCalendarSettingView.h"
@implementation DateArrangeNavigationController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}
- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.view.backgroundColor = [UIColor clearColor];
    LDCMyDateArrangeViewController *addPatientVC=[[LDCMyDateArrangeViewController alloc] init];
    self.viewControllers = @[addPatientVC];
}
@end

#define TitleViewHeight (20+20+45)/2
#define scrollHeight 500

@interface LDCMyDateArrangeViewController ()<KDTitleViewDelegate,KDCalendarPickerDelegate,KDAddNumberDelegate,KDCalendarSettingDelegate>
{
    UIScrollView    *_scrollView;
    UIScrollView    *_scrollView2;
    KDTitleView     *_titleView;
    KDCalendarPicker    *calendarPicker;
    KDAddNumberView     *_addNumberView;
    KDCalendarSettingView   *_calendarSettingView;
    NSString        *_currentDateStr;
}
@property (nonatomic , strong)NSString  *currentDateStr;
@end

@implementation LDCMyDateArrangeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
     [self setNaviTitle:@"我的日程"];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor colorWithHexString:@"0xF2F2F2"];
    UISegmentedControl *seg = [[UISegmentedControl alloc] initWithItems:@[@"日程表",@"出诊设置"]];
    seg.frame = CGRectMake(0, 10, 152, 23);
    seg.selectedSegmentIndex = 0;
    [self.view addSubview:seg];
    seg.center = CGPointMake(self.view.center.x, seg.center.y);
    
    
    _titleView = [[KDTitleView alloc] init];
    _titleView.clickDelegate = self;
    [self.view addSubview:_titleView];
    
    
    
    [self createUIFirst];
    [self createUISecond];
}

//网络交互
//请求可加号天数
- (void)requestSchedule:(NSString *)open_date
{
    if (open_date) {
        [CSHApiClient displayRiliOpen_date:open_date DidFinishLoaded:^(KXJson *json, NSString *path) {
            //
            [calendarPicker reloadCollectionViewData:(NSArray *)[json getObjectForKey:@"data"]];
        } didFailLoaded:^(NSError *error, NSString *path) {
            //
        }];
    }
}
//请求某天可加号数
- (void)requestScheduleDay:(NSString *)open_date
{
    if (open_date) {
        [CSHApiClient scheduleDayOpen_date:open_date DidFinishLoaded:^(KXJson *json, NSString *path) {
            //
            [_addNumberView refeshData:(NSArray *)[json getObjectForKey:@"data"]];
        } didFailLoaded:^(NSError *error, NSString *path) {
            //
        }];
    }
}
//提交某天加号数
- (void)requestOurcallPeopleNum:(NSString *)open_date open_time:(NSString *)open_time open_num:(NSString *)open_num
{
    if (open_date) {
        [CSHApiClient ourcallPeopleNumOpen_date:open_date open_time:open_time open_num:open_num didFinishLoaded:^(KXJson *json, NSString *path) {
            //
            NSDictionary *dic1 = [NSDictionary dictionaryWithDictionary:[json getObjectForKey:@"data"]];
            
            if ([[dic1 objectForKey:@"state"] intValue]== 1) {
                [self showAlert:@"添加成功"];
            }else{
                [self showAlert:@"添加失败请稍后重试"];
            }
        } didFailLoaded:^(NSError *error, NSString *path) {
            //
        }];
    }
}

//出诊设置查询
- (void)requestOurcallSet
{
        [CSHApiClient ourcallsetDidFinishLoaded:^(KXJson *json, NSString *path) {
            //
            NSDictionary *dic1 = [NSDictionary dictionaryWithDictionary:[json getObjectForKey:@"data"]];
            [_calendarSettingView reloadCollectionView:dic1];
        } didFailLoaded:^(NSError *error, NSString *path) {
            //
        }];
}
//出诊设置确定
- (void)requestOutcallConfirm:(NSString *)return_set_outcall start_date:(NSString *)start_date end_date:(NSString *)end_date
{
    [CSHApiClient outcallConfirmReturn_set_outcall:return_set_outcall DidFinishLoaded:^(KXJson *json, NSString *path) {
        //
        NSDictionary *dic1 = [NSDictionary dictionaryWithDictionary:[json getObjectForKey:@"data"]];
        
        if ([[dic1 objectForKey:@"state"] intValue]== 1) {
            [self showAlert:@"设置成功"];
        }else{
            [self showAlert:@"添加设置请稍后重试"];
        }
    } didFailLoaded:^(NSError *error, NSString *path) {
        //
    }];
}

- (void)createUIFirst
{
    _scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, Y(_titleView)+HEIGHT(_titleView), kScreenWidth, kScreenHeight - kStatusBarHeight - kNavigationBarHeight - TitleViewHeight - kToolBarHeight)];
    _scrollView.backgroundColor = [UIColor whiteColor];
    _scrollView.contentSize = CGSizeMake(kScreenWidth,590);
    [self.view addSubview:_scrollView];
    
    UIView  *calendarView = [[UIView alloc] initWithFrame:(CGRect){0,0,kScreenWidth,284}];
    [calendarView setUserInteractionEnabled:YES];
    [calendarView setBackgroundColor:[UIColor clearColor]];
    [_scrollView addSubview:calendarView];
    
    calendarPicker = [KDCalendarPicker showOnView:calendarView];
    calendarPicker.pickerDelegate = self;
    calendarPicker.today = [NSDate date];
    calendarPicker.date = calendarPicker.today;
    
    _addNumberView = [[KDAddNumberView alloc] initWithFrame:(CGRect){0,Y(calendarView)+HEIGHT(calendarView),kScreenWidth,_scrollView.contentSize.height-HEIGHT(calendarView)}];
    _addNumberView.addNumberDelegate = self;
    [_scrollView addSubview:_addNumberView];
}

- (void)refeshAddNumberViewDate:(NSString *)dateStr
{
    _addNumberView.morningLabel.text = [NSString stringWithFormat:@"%@ 上午门诊",dateStr];
    _addNumberView.afternoonLabel.text = [NSString stringWithFormat:@"%@ 下午门诊",dateStr];
    _addNumberView.eveningLabel.text = [NSString stringWithFormat:@"%@ 晚上门诊",dateStr];
}

- (void)createUISecond
{
    _scrollView2 = [[UIScrollView alloc] initWithFrame:CGRectMake(0, Y(_titleView)+HEIGHT(_titleView), kScreenWidth, kScreenHeight - kStatusBarHeight - kNavigationBarHeight - TitleViewHeight - kToolBarHeight)];
    _scrollView2.backgroundColor = [UIColor whiteColor];
    _scrollView2.contentSize = CGSizeMake(kScreenWidth,475);
    [self.view addSubview:_scrollView2];
    _scrollView2.hidden = YES;
    
    _calendarSettingView = [[KDCalendarSettingView alloc] initWithFrame:(CGRect){0,0,kScreenWidth,83/2*7+58/2+20*2+35/2+12+15*2+34/2+70/2+20}];
    _calendarSettingView.calendarSettingDelegate = self;
    [_scrollView2 addSubview:_calendarSettingView];
    
}
//delegate
- (void)buttonClick:(int)whichButton
{
    switch (whichButton) {
        case 1:{
            _scrollView2.hidden = YES;
            _scrollView.hidden = NO;
            
        }break;
        case 2:{
            _scrollView.hidden = YES;
            _scrollView2.hidden = NO;
            [self requestOurcallSet];
        }break;
        default:
            break;
    }
}
- (void)pickerClickDay:(NSInteger)day Month:(NSInteger)month Year:(NSInteger)year
{
    NSString    *dateStr = [NSString stringWithFormat:@"%li月%li日",(long)month,(long)day];
    self.currentDateStr = [NSString stringWithFormat:@"%li%li%li",(long)year,(long)month,(long)day];
    [self requestScheduleDay:_currentDateStr];
    [self refeshAddNumberViewDate:dateStr];
}
- (void)chooseCurrentDateMonth:(NSInteger)month Year:(NSInteger)year
{
    [self requestSchedule:[NSString stringWithFormat:@"%li%li",(long)year,(long)month]];
}
- (void)chooseResult:(NSDictionary *)resultDic
{
    NSString *numStr = [NSString stringWithFormat:@"%@,%@,%@",resultDic[@"0"],resultDic[@"1"],resultDic[@"2"]];
    [self requestOurcallPeopleNum:_currentDateStr open_time:@"0，1，2" open_num:numStr];
}

- (void)calendarSettingSureButton:(NSString *)return_set_outcall startDate:(NSString *)startDate endDate:(NSString *)endDate
{
    [self requestOutcallConfirm:return_set_outcall start_date:startDate end_date:endDate];
}

- (void)showAlert:(NSString *)str
{
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"温馨提示" message:str delegate:nil cancelButtonTitle:@"确定" otherButtonTitles: nil];
    [alert show];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
